// 定义心跳函数
export function startHeartbeat(socket, uid) {
	const heartbeatInterval = setInterval(() => {
		sendHeartbeat(socket, uid);
	}, 14000);

	// 发送心跳消息
	function sendHeartbeat(socket, uid) {
		if (socket && socket.readyState === WebSocket.OPEN) {
			const msg = {
				uid: uid,
				msg_type: 'heartbeat'
			};
			socket.send(JSON.stringify(msg));
		} else {
			console.warn('WebSocket is not open.');
		}
	}

	// 在组件销毁时清除心跳定时器
	return function stopHeartbeat() {
		clearInterval(heartbeatInterval);
	};
}